//package com.example.springai.langchat;
//
//import dev.ai4j.openai4j.OpenAiClient;
//import dev.ai4j.openai4j.SyncOrAsync;
//import dev.ai4j.openai4j.image.GenerateImagesRequest;
//import dev.ai4j.openai4j.image.GenerateImagesResponse;
//import dev.langchain4j.data.image.Image;
//import dev.langchain4j.internal.RetryUtils;
//import dev.langchain4j.internal.Utils;
//import dev.langchain4j.model.image.ImageModel;
//import dev.langchain4j.model.output.Response;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//
//import java.net.Proxy;
//import java.nio.file.Path;
//import java.time.Duration;
//import java.util.List;
//import java.util.Map;
//import java.util.stream.Collectors;
//
//public class SiliconflowImageModel implements ImageModel {
//    private static final Logger logger = LoggerFactory.getLogger(SiliconflowImageModel.class);
//
//    private final String baseUrl;
//    private final String apiKey;
//    private final String modelName;
//    private final String size;
//    private final String quality;
//    private final String style;
//    private final String user;
//    private final OpenAiClient client;
//    private final Integer maxRetries;
//
//    // Constructor
//    private SiliconflowImageModel(String baseUrl, String apiKey, String modelName, String size, String quality, String style, String user, Duration timeout, Integer maxRetries, Proxy proxy, Boolean logRequests, Boolean logResponses, Boolean withPersisting, Path persistTo, Map<String, String> customHeaders) {
//        this.baseUrl = Utils.getOrDefault(baseUrl, "https://api.siliconflow.cn/v1");
//        this.timeout = Utils.getOrDefault(timeout, Duration.ofSeconds(60L));
//        OpenAiClient.Builder clientBuilder = OpenAiClient.builder()
//            .baseUrl(this.baseUrl)
//            .openAiApiKey(apiKey) // Assuming Siliconflow uses a similar Bearer token
//            .callTimeout(this.timeout)
//            .connectTimeout(this.timeout)
//            .readTimeout(this.timeout)
//            .writeTimeout(this.timeout)
//            .proxy(proxy)
//            .logRequests(Utils.getOrDefault(logRequests, false))
//            .logResponses(Utils.getOrDefault(logResponses, false))
//            .userAgent("langchain4j-siliconflow")
//            .persistTo(persistTo)
//            .customHeaders(customHeaders);
//
//        if (withPersisting != null && withPersisting) {
//            clientBuilder.withPersisting();
//        }
//
//        this.client = clientBuilder.build();
//        this.apiKey = apiKey;
//        this.modelName = modelName;
//        this.size = size;
//        this.quality = quality;
//        this.style = style;
//        this.user = user;
//        this.maxRetries = Utils.getOrDefault(maxRetries, 3);
//    }
//
//    private final Duration timeout;
//
//    public String modelName() {
//        return this.modelName;
//    }
//
//    @Override
//    public Response<Image> generate(String prompt) {
//        GenerateImagesRequest request = requestBuilder(prompt).build();
//        GenerateImagesResponse response = executeRequest(request);
//        return Response.from(fromImageData(response.data().get(0)));
//    }
//
//    @Override
//    public Response<List<Image>> generate(String prompt, int n) {
//        GenerateImagesRequest request = requestBuilder(prompt).n(n).build();
//        GenerateImagesResponse response = executeRequest(request);
//        return Response.from(response.data().stream().map(this::fromImageData).collect(Collectors.toList()));
//    }
//
//    private GenerateImagesResponse executeRequest(GenerateImagesRequest request) {
//        return (GenerateImagesResponse) RetryUtils.withRetry(() -> {
//
//                logger.debug("Request: {}", request.toString());
//            SyncOrAsync<GenerateImagesResponse> syncOrAsync = client.imagesGeneration(request);
//            GenerateImagesResponse response = syncOrAsync.execute();
//                logger.debug("Response: {}", response.toString());
//            return response;
//        }, this.maxRetries);
//    }
//
//    private GenerateImagesRequest.Builder requestBuilder(String prompt) {
//        GenerateImagesRequest.Builder requestBuilder = GenerateImagesRequest.builder()
//            .prompt(prompt)
//            .model(this.modelName) // Explicitly use the provided model name
//            .size(this.size)
//            .quality(this.quality)
//            .style(this.style)
//            .user(this.user);
//
//        return requestBuilder;
//    }
//
//    private Image fromImageData(dev.ai4j.openai4j.image.ImageData data) {
//        return Image.builder()
//            .url(data.url())
//            .base64Data(data.b64Json())
//            .revisedPrompt(data.revisedPrompt())
//            .build();
//    }
//
//    // Builder pattern
//    public static Builder builder() {
//        return new Builder();
//    }
//
//    public static class Builder {
//        private String baseUrl;
//        private String apiKey;
//        private String modelName;
//        private String size;
//        private String quality;
//        private String style;
//        private String user;
//        private Duration timeout;
//        private Integer maxRetries;
//        private Proxy proxy;
//        private Boolean logRequests;
//        private Boolean logResponses;
//        private Boolean withPersisting;
//        private Path persistTo;
//        private Map<String, String> customHeaders;
//
//        private Builder() {
//        }
//
//        public Builder baseUrl(String baseUrl) {
//            this.baseUrl = baseUrl;
//            return this;
//        }
//
//        public Builder apiKey(String apiKey) {
//            this.apiKey = apiKey;
//            return this;
//        }
//
//        public Builder modelName(String modelName) {
//            this.modelName = modelName;
//            return this;
//        }
//
//        public Builder size(String size) {
//            this.size = size;
//            return this;
//        }
//
//        public Builder quality(String quality) {
//            this.quality = quality;
//            return this;
//        }
//
//        public Builder style(String style) {
//            this.style = style;
//            return this;
//        }
//
//        public Builder user(String user) {
//            this.user = user;
//            return this;
//        }
//
//        public Builder timeout(Duration timeout) {
//            this.timeout = timeout;
//            return this;
//        }
//
//        public Builder maxRetries(Integer maxRetries) {
//            this.maxRetries = maxRetries;
//            return this;
//        }
//
//        public Builder proxy(Proxy proxy) {
//            this.proxy = proxy;
//            return this;
//        }
//
//        public Builder logRequests(Boolean logRequests) {
//            this.logRequests = logRequests;
//            return this;
//        }
//
//        public Builder logResponses(Boolean logResponses) {
//            this.logResponses = logResponses;
//            return this;
//        }
//
//        public Builder withPersisting(Boolean withPersisting) {
//            this.withPersisting = withPersisting;
//            return this;
//        }
//
//        public Builder persistTo(Path persistTo) {
//            this.persistTo = persistTo;
//            return this;
//        }
//
//        public Builder customHeaders(Map<String, String> customHeaders) {
//            this.customHeaders = customHeaders;
//            return this;
//        }
//
//        public SiliconflowImageModel build() {
//            if (apiKey == null || apiKey.isEmpty()) {
//                throw new IllegalArgumentException("API key must be provided");
//            }
//            return new SiliconflowImageModel(baseUrl, apiKey, modelName, size, quality, style, user, timeout, maxRetries, proxy, logRequests, logResponses, withPersisting, persistTo, customHeaders);
//        }
//    }
//
//    // Example usage
//    public static void main(String[] args) {
//        SiliconflowImageModel model = SiliconflowImageModel.builder()
//            .baseUrl("https://api.siliconflow.cn/v1")
//            .apiKey("sk-nrvjihoykgbjabnelziszukgkcankraqcwtvohvpcuepuyyz")
//            .modelName("black-forest-labs/FLUX.1-dev")
//            .size("1024x1024")
//
//            .logRequests(true)
//            .logResponses(true)
//            .build();
//        Response<Image> response = model.generate("画一个小帅哥");
//        System.out.println(response.content().url());
//    }
//}